package com.monkey.imageselect;

import ohos.aafwk.content.Intent;
import ohos.ace.ability.AceAbility;
import ohos.security.SystemPermission;

import static ohos.bundle.IBundleManager.PERMISSION_GRANTED;

public class MainAbility extends AceAbility {

    public static final int STORAGE_PERMISSION_REQUEST_CODE = 99;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        ImageDataInternalAbility.getInstance().onRegister(this);
        requestPermissionAndDisplayGallery();
    }


    private void requestPermissionAndDisplayGallery() {
        if (verifyCallingOrSelfPermission(SystemPermission.READ_USER_STORAGE) != PERMISSION_GRANTED
//                || verifyCallingOrSelfPermission(SystemPermission.WRITE_USER_STORAGE) != PERMISSION_GRANTED
        ) {
            requestPermissionsFromUser(new String[]{SystemPermission.READ_USER_STORAGE}, STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        if (requestCode == STORAGE_PERMISSION_REQUEST_CODE) {
            if (grantResults[0] == PERMISSION_GRANTED) {
                //已授权
            } else {
                //未授权
            }
        }
    }


    public void load() {


    }


    @Override
    public void onStop() {
        super.onStop();
        ImageDataInternalAbility.getInstance().onUnregister();
    }
}
